﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Finds the brightest points in an image
    /// </summary>
    public class ExperimentalMaximumPercentile : BaseForeBackgroundColorFilter
    {
        /// <summary>
        /// This is considered as starting value, 
        /// </summary>
        protected float m_fMaxThreshold = 20000.0f;
        protected ColorCalculation colorCalculation = null;

        /// <summary>
        /// 
        /// </summary>
        public ExperimentalMaximumPercentile()
        {
            this.M_PROCESSING_MODE = PROCESSING_MODE.Color;
            colorCalculation = Definitions.ColorCalculationDictionaryProcessingMode[M_PROCESSING_MODE];
        }

        /// <summary>
        /// This constructor is used by cloning this instance
        /// </summary>
        /// <param name="_experimentalMaximumPercentile"></param>
        internal ExperimentalMaximumPercentile(ExperimentalMaximumPercentile _experimentalMaximumPercentile)
        {
            m_fMaxThreshold = _experimentalMaximumPercentile.m_fMaxThreshold;
        }

        /// <summary>
        /// Gets or sets the threshold
        /// </summary>
        public float Threshold
        {
            get { return m_fMaxThreshold; }
            set { m_fMaxThreshold = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int y = 0;
            int x = 0;
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            int nMax = -1;

            if (M_PROCESSING_MODE != PROCESSING_MODE.Color)
            {
                Color color = Color.Transparent;
                for (y = 0; y < nHeight; y++)
                {
                    for (x = 0; x < nWidth; x++)
                    {
                        color = _bitmap.GetPixel(x, y);
                        if (colorCalculation(color) > nMax) nMax = (int)colorCalculation(color);
                    }
                }

                for (y = 0; y < nHeight; y++)
                {
                    for (x = 0; x < nWidth; x++)
                    {
                        color = _bitmap.GetPixel(x, y);
                        _bitmap.SetPixel(x, y, colorCalculation(color) >= nMax - m_fMaxThreshold ? m_ColorForeground : m_ColorBackground);
                    }
                }
            }
            else
            {
                for (y = 0; y < nHeight; y++)
                {
                    for (x = 0; x < nWidth; x++)
                    {
                        int nValue = _bitmap.GetPixelDirectInt(x, y);
                        if (nValue > nMax) nMax = nValue;
                    }
                }

                for (y = 0; y < nHeight; y++)
                {
                    for (x = 0; x < nWidth; x++)
                    {
                        int nValue = _bitmap.GetPixelDirectInt(x, y);
                        _bitmap.SetPixel(x, y, nValue >= nMax - m_fMaxThreshold ? m_ColorForeground : m_ColorBackground);
                    }
                }
            }

            return _bitmap;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ExperimentalMaximumPercentile(this);
        }
    }
}
